//
//  Hen_ConfigDBHelper.m
//  OnlineRetailers
//
//  Created by mini2 on 2017/6/2.
//  Copyright © 2017年 OnlineRetailers. All rights reserved.
//

#import "Hen_ConfigDBHelper.h"
#import <sqlite3.h>

//配置表
#define HenConfigInfo          @"hen_config_info"

@interface Hen_ConfigDBHelper(){
    sqlite3 *db;
}

@end

@implementation Hen_ConfigDBHelper

-(id)init
{
    self = [super init];
    if(self){
        [self initDefault];
    }
    
    return self;
}

///初始化
-(void)initDefault
{
    //打开数据库
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [paths objectAtIndex:0];
    NSString *database_path = [documents stringByAppendingPathComponent:@"user.sqlite"];
    
    if (sqlite3_open([database_path UTF8String], &db) != SQLITE_OK) {
        sqlite3_close(db);
        NSLog(@"数据库打开失败");
    }
    
    //配置数据
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE %@('token' TEXT, 'StartAppCount' TEXT, 'mobile' TEXT, 'remarks' TEXT, 'shopType' TEXT)", HenConfigInfo];
    if([self execSql:sql]){
        // 插入配置数据
        NSString *insertConfigSql = [NSString stringWithFormat:@"INSERT INTO %@ (token,StartAppCount,mobile,remarks,shopType) VALUES ('','','','', '')", HenConfigInfo];
        [self execSql:insertConfigSql];
    }
}

///执行sql语句
-(BOOL)execSql:(NSString*)sql
{
    char *err;
    if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
        NSLog(@"数据库操作数据失败! -- %s", err);
        return NO;
    }
    return YES;
}

///获取Token
- (NSString *)getToken
{
    NSString *result = @"";
    
    NSString *sql = [NSString stringWithFormat:@"SELECT token FROM %@", HenConfigInfo];
    sqlite3_stmt *stmt = NULL;
    int status = sqlite3_prepare(db, [sql UTF8String], -1, &stmt, NULL);
    if (status == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            result = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
        }
        sqlite3_finalize(stmt);
    } else {
        sqlite3_finalize(stmt);
    }
    
    return result;
}

///获取启动次数
- (NSString *)getStartAppCount
{
    NSString *result = @"";
    
    NSString *sql = [NSString stringWithFormat:@"SELECT StartAppCount FROM %@", HenConfigInfo];
    sqlite3_stmt *stmt = NULL;
    int status = sqlite3_prepare(db, [sql UTF8String], -1, &stmt, NULL);
    if (status == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            result = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
        }
        sqlite3_finalize(stmt);
    } else {
        sqlite3_finalize(stmt);
    }
    
    return result;
}
- (NSString *)getSHopType{
    NSString *result = @"";
    
    NSString *sql = [NSString stringWithFormat:@"SELECT shopType FROM %@", HenConfigInfo];
    sqlite3_stmt *stmt = NULL;
    int status = sqlite3_prepare(db, [sql UTF8String], -1, &stmt, NULL);
    if (status == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            result = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
        }
        sqlite3_finalize(stmt);
    } else {
        sqlite3_finalize(stmt);
    }
    
    return result;

}
///获取手机号
- (NSString *)getMobile
{
    NSString *result = @"";
    
    NSString *sql = [NSString stringWithFormat:@"SELECT mobile FROM %@", HenConfigInfo];
    sqlite3_stmt *stmt = NULL;
    int status = sqlite3_prepare(db, [sql UTF8String], -1, &stmt, NULL);
    if (status == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            result = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
        }
        sqlite3_finalize(stmt);
    } else {
        sqlite3_finalize(stmt);
    }
    
    return result;


}

///更新token
- (void)updateToken:(NSString *)token
{
    if(!token){
        token = @"";
    }
    
    //更新
    NSString *updateSql = [NSString stringWithFormat:@"update %@ set token = '%@'", HenConfigInfo, token];
    [self execSql:updateSql];
}

///更新启动次数
- (void)updateAppStartCount
{
    //更新
    NSString *updateSql = [NSString stringWithFormat:@"update %@ set StartAppCount = 1", HenConfigInfo];
    [self execSql:updateSql];
}

///更新手机号
- (void)updateMobile:(NSString *)mobile;
{
    if(!mobile){
        mobile = @"";
    }
    //更新
    NSString *updateSql = [NSString stringWithFormat:@"update %@ set mobile = '%@'", HenConfigInfo, mobile];
    [self execSql:updateSql];
    
}
- (void)updateShopType:(NSString *)shopType{
    if(!shopType){
        shopType = @"";
    }
    //更新
    NSString *updateSql = [NSString stringWithFormat:@"update %@ set shopType = '%@'", HenConfigInfo, shopType];
    [self execSql:updateSql];
}
@end
